<template>
  <div id="setMeal">
    <!--搜索-->
    <div class="search-content">
      <div>
        <el-select v-model="vipType" clearable placeholder="请选择会员类型"  @change="inquiryList" >
          <el-option
            v-for="item in vipTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-select v-model="enable" clearable placeholder="请选择是否启用"  @change="inquiryList" >
          <el-option
            v-for="item in enableList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-button type="primary" icon="el-icon-search" @click="inquiryList">查询</el-button>
      </div>
      <div class="search-apply">
        <el-button type="warning" icon="el-icon-edit" @click="addUpload">新增</el-button>
      </div>
    </div>
    <!--操作列表-->
    <div class="table">
      <el-table fit :data="tableData" style="width: 100%">
        <el-table-column label="会员类型" prop="vipType"  align="center">
          <template slot-scope="scope">
            <el-tag size="small" v-if="scope.row.vipType==='blueDiamond'">蓝钻VIP</el-tag>
            <el-tag size="small" v-if="scope.row.vipType==='anCrown'" type="warning">皇冠VIP</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="月份" prop="month"  align="center" />
        <el-table-column label="价格" prop="price"  align="center" />
        <el-table-column label="是否启用" prop="enable"  align="center">
          <template slot-scope="scope">
            <span>{{scope.row.enable?'启用':'不启用'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createDate" width="170"  align="center" />
        <el-table-column label="操作"  width="300" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="dealwithEdit(scope.$index, scope.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="dealwithDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="pagination">
        <el-pagination
          :current-page.sync="currentPage"
          :page-sizes="[10,20,30,40,50]"
          layout="total,sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!--新增蒙层-->
    <el-dialog id="add-dialog" title="增加会员套餐" :visible.sync="addDialogVisible">
      <el-form label-position="right" label-width="120px"  :model="add"  :rules="addRules" ref="addRuleForm">
        <el-form-item label="会员类型：">
          <el-select v-model="add.vipType" placeholder="请选择会员类型" >
            <el-option
              v-for="item in vipTypeList2"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="套餐月数：" prop="month">
          <el-input v-model="add.month" placeholder="请输入套餐月数" clearable />
        </el-form-item>
        <el-form-item label="价格：" prop="price">
          <el-input v-model="add.price" placeholder="请输入价格" clearable />
        </el-form-item>
        <el-form-item label="是否启用：">
          <el-switch v-model="add.enable" inactive-text="不启用" active-text="启用"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button id="add-btn" type="primary" @click="addSubmit('addRuleForm')">新 增</el-button>
      </span>
    </el-dialog>
    <!--修改蒙层-->
    <el-dialog id="edit-dialog" title="修改会员套餐" :visible.sync="editDialogVisible">
      <el-form label-position="right" label-width="120px"  :model="edit"  :rules="editRules" ref="editRuleForm">
        <el-form-item label="会员类型：">
          <el-select v-model="edit.vipType" placeholder="请选择会员类型">
            <el-option
              v-for="item in vipTypeList2"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="套餐月数：" prop="month">
          <el-input v-model="edit.month" placeholder="请输入套餐月数" clearable />
        </el-form-item>
        <el-form-item label="价格：" prop="price">
          <el-input v-model="edit.price" placeholder="请输入价格" clearable />
        </el-form-item>
        <el-form-item label="是否启用：">
          <el-switch v-model="edit.enable" inactive-text="不启用" active-text="启用"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button id="add-btn" type="primary" @click="editSubmit('editRuleForm')">修 改</el-button>
      </span>
    </el-dialog>
    <!--删除-->
    <el-dialog :visible.sync="delDialogVisible" width="300px" center id="del-dialog" top="20%">
      <span>您确定要删除吗？</span>
      <span slot="footer" class="dialog-footer">
          <el-button @click="delDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="delKeys">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
  import {Message} from 'element-ui';
  import request from "../../utils/request";
  import axios from 'axios'
  export default {
    name: "setMealList",
    data(){
      var checkPublic = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('礼物价格不能为空'));
        }else if(!/^[1-9]\d*$/.test(value)){
          return callback(new Error('礼物价格只能是正整数'));
        }else {
          callback();
        }
      };
      return{
        // 查询
        vipType:null,
        vipTypeList:[
          {
            value:1,
            label:"蓝钻VIP"
          },
          {
            value:2,
            label:"皇冠VIP"
          }
        ],
        enable:null,
        enableList:[
          {
            value:true,
            label:"启用"
          },
          {
            value:false,
            label:"不启用"
          }
        ],
        // 列表数据
        tableData:[],
        //新增
        vipTypeList2:[
          {
            value:'blueDiamond',
            label:"蓝钻VIP"
          },
          {
            value:'anCrown',
            label:"皇冠VIP"
          }
        ],
        addDialogVisible:false,
        add:{
          vipType:'blueDiamond',
          month:'',
          price:'',
          enable:true
        },
        addRules:{
          month:[{validator: checkPublic, trigger: 'blur' }],
          price:[{validator: checkPublic, trigger: 'blur' }]
        },
        //修改
        editDialogVisible:false,
        edit:{},
        editRules:{
          month:[{validator: checkPublic, trigger: 'blur' }],
          price:[{validator: checkPublic, trigger: 'blur' }]
        },
        // 删除
        delId:null,
        delDialogVisible:false,
        //分页
        currentPage: 1,
        pageSize:10,
        total: 0
      }
    },
    mounted() {
      this.inquiryList();
    },
    methods:{
      isNotNull(value){
        return value !== null && value !== undefined && value !== "";
      },
      //查询列表
      inquiryList(){
        let that=this;
        that.tableData=[];
        let data={
          pageNumber:that.currentPage,
          pageSize:that.pageSize,
          filters:[]
        };
        if(this.isNotNull(that.vipType)){  //会员类型
          data.filters.push({
            property:"vip_type",
            value:that.vipType,
            operator: "eq"
          })
        }
        if(this.isNotNull(that.enable)){ //是否启用
          data.filters.push({
            property:"enable",
            value:that.enable,
            operator: "eq"
          })
        }
        request({
          method: 'post',
          url: 'admin/setMeal/list',
          headers:{
            'Content-Type': 'application/json;charset=UTF-8'
          },
          data:data
        }).then(function (res) {
          if(res.code===200){
            that.total=res.data.total;
            let arr=res.data.records;
            if(arr.length===0){
              that.tableData=[];
            }else{
              that.tableData=arr
            }
          }
        }).catch(function (error) {
          console.log(error);
        });
      },
      //新增
      addUpload(){
        this.addDialogVisible=true;
      },
      addSubmit(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let that=this;
            request({
              method: 'post',
              url: 'admin/setMeal/save',
              data:that.add
            }).then(function (res) {
              if(res.code===200){
                that.addDialogVisible = false;
                Message.success("新增成功");
                that.$refs['addRuleForm'].resetFields();
                that.inquiryList();
              }
            }).catch(function (error) {
              console.log(error);
            });
          }else {
            return false;
          }
        });
      },
      //修改
      dealwithEdit(index, row) {
        this.editDialogVisible = true;
        this.edit = row;
      },
      editSubmit(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let that=this;
            delete that.edit.createDate;
            delete that.edit.modifyDate;
            request({
              method: 'post',
              url: 'admin/setMeal/update',
              data:that.edit
            }).then(function (res) {
              if(res.code===200){
                that.editDialogVisible = false;
                Message.success("修改成功");
                that.inquiryList();
              }
            }).catch(function (error) {
              console.log(error);
            });
          }else {
            return false;
          }
        });
      },
      //删除
      dealwithDelete(index, row){
        this.delDialogVisible=true;
        this.delId=row.id;
      },
      delKeys(){
        let that=this;
        that.delDialogVisible=false;
        request({
          method: 'post',
          url: 'admin/setMeal/delete/'+that.delId,
        }).then(function (res) {
          if(res.code===200){
            Message.success("删除成功");
            that.delId=null;
            that.inquiryList();
          }
        }).catch(function (error) {
          console.log(error);
        });
      },
      // 改变每页显示的数量
      handleSizeChange(val) {
        this.pageSize=val;
        this.inquiryList();
      },
      //改变当前页
      handleCurrentChange(val) {
        this.currentPage=val;
        this.inquiryList();
      }
    }
  }
</script>

<style scoped src="../../assets/css/setMealManagement/setMealList.css">

</style>
